<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/19 13:54
 * Desc:
 */
class Model_Api_V2_Integral_Mall extends ORM
{
    const TYPE_ID = 107;

    /**
     * @function 获取数据
     * @param $offset
     * @param $limit
     * @param int $attr_id
     * @return mixed
     */
    public static function get_integral($offset, $limit, int $attr_id = 0)
    {
        $builder = DB::select('id', 'title', 'litpic', 'need_jifen','number','member_max_number')
            ->from('integral');
        if ($attr_id) {
            $builder->where('mall_type_id', '=', $attr_id);
        }
        return $builder->and_where('ishidden', '=', 0)
            ->and_where('number', '>', 0)
            ->order_by('displayorder', 'asc')
            ->order_by('modtime', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->execute()
            ->as_array();
    }

    /**
     * 积分兑换商品
     * @param $mid
     * @param $params
     * @return mixed|null
     * @throws Kohana_Exception
     * @throws Exception
     */
    public static function exchange($mid, $params)
    {
        /**
         * @var $id
         * @var $info
         * @var $book_num //预订数量
         * @var $linkman //联系人姓名
         * @var $linktel //联系人手机号
         * @var $linkemail //联系人邮箱
         * @var $remark //备注
         * @var $receive_address_id //收货地址id
         */
        extract($params);

        //减数量
        DB::update('integral')
            ->set(array('number' => DB::expr("number-{$book_num}")))
            ->where('id', '=', intval($id))
            ->execute();

        //下单成功
        $data = array(
            'webid' => 0,
            'price' => 0,
            'status' => 5,
            'usejifen' => 1,
            'paytype' => 1,
            'ispinlun' => 1,
            'paytime' => time(),
            'addtime' => time(),
            'paysource' => '积分兑换',
            'ordersn' => Product::get_ordersn(Model_Api_V2_Integral_Mall::TYPE_ID),
            'needjifen' => $book_num * $info['need_jifen'],
            'litpic' => $info['litpic'],
            'typeid' => Model_Api_V2_Integral_Mall::TYPE_ID,
            'memberid' => $mid,
            'productaid' => $info['id'],
            'productautoid' => $info['id'],
            'productname' => $info['title'],
            'dingnum' => $book_num,
            'remark' => serialize(['book' => $remark]),
            'linkman' => htmlentities($linkman),
            'linktel' => htmlentities($linktel),
            'linkemail' => htmlentities($linkemail),
            'receiver_address_id' => $receive_address_id ?? '',
        );

        //下单
        list($id, $row) = DB::insert('member_order', array_keys($data))
            ->values(array_values($data))
            ->execute();

        if (!$row) {
            throw new Exception('用户下单失败');
        }

        //扣除积分并写入日志
        DB::update('member')
            ->set(array('jifen' => DB::expr('jifen-' . $data['needjifen'])))
            ->where('mid', '=', $mid)
            ->execute();

        $content = "兑换产品<{$data['productname']}>使用{$data['needjifen']}积分";
        St_Product::add_jifen_log($mid, $content, $data['needjifen'], 1);

        return $id;
    }
}